﻿using System.Web.Mvc;
using ECG.Common;
using ECG.DAL;
using ECG.WebUI.CustomHelpers;

namespace ECG.WebUI.Controllers
{
    public class CampaignController : Controller
    {
        //
        // GET: /Campaign/

        #region Chiến dịch 20 - 11

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(CampaignCustomerDto model)
        {
            if(ModelState.IsValid)
            {
                var checkEmailExist = CampaignCustomerDb.GetByEmail(model.Email) ?? new CampaignCustomerDto();
                if(!string.IsNullOrEmpty(checkEmailExist.Email))
                {
                    ModelState.AddModelError("ErrorSummary","Email này đã tồn tại trên hệ thống, bạn hãy nhập một Email khác!");
                    return View(model);
                }
                // Insert
                model.MaXoSo = FunctionHelpers.GenerateId();
                CampaignCustomerDb.Add(model);

                // Send mail
                var resultSendMail = FunctionHelpers.SendMailDangKyQuayXoSo(model.Name, model.Email, model.MaXoSo);
                if(resultSendMail)
                {
                    return RedirectToAction("Announce", new { success = true });
                }
            }
            return View(model);
        }

        public PartialViewResult ProductPromotionList()
        {
            return PartialView();
        }

        public ActionResult Announce(bool success = false)
        {
            ViewBag.Success = success;
            return View();
        }

        #endregion
    }
}
